package com.quan.admin.web.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.quan.admin.commons.vo.SysDictVo;
import com.quan.admin.entity.SysDict;
import com.quan.admin.entity.SysDictDetail;
import com.quan.admin.service.SysDictDetailService;
import com.quan.admin.service.SysDictService;
import com.quan.commons.core.bean.R;
import com.quan.commons.core.biz.support.MyBaseController;
import com.quan.commons.core.utils.PageUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
 * 系统-字典
 *
 * @author yhaoquan
 * @email yhaoquan@163.com
 * @date 2020-08-08 20:15:07
 */
@Api(tags = "系统-字典")
@RestController
@RequestMapping("sysdict")
public class SysDictController extends MyBaseController {

    @Autowired
    private SysDictService service;

    @Autowired
    private SysDictDetailService sysDictDetailService;

    /**
     * 自定义分页列表
     * @param entity
     * @return
     */
    @GetMapping(value = "selectMyPage")
    public Object selectMyPage(SysDict entity) {
        IPage<SysDict> page = this.service.selectMyPage(new PageUtils(request), entity);
        return R.ok().put("data", page);
    }

    /**
     * 分页列表
     * @param entity
     * @return
     */
    @GetMapping(value = "page")
    public Object page(SysDict entity) {
        IPage<SysDict> page = this.service.selectPage(new PageUtils(request), entity);
        return R.ok().put("data", page);
    }

    /**
     * 列表
     * @param entity
     * @return
     */
    @GetMapping(value = "list")
    public Object list(SysDict entity) {
        List<SysDict> list = this.service.list(new PageUtils(request), entity);
        return R.ok().put("data", list);
    }


    /**
     * 信息
     * @param {id}
     * @return
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		SysDict sysDict = this.service.getById(id);
        return R.ok().put("data", sysDict);
    }

    /**
     * 保存
     * @param entity
     * @param bindingResult
     * @return
     */
    @PostMapping("save")
    public R save(@Valid @RequestBody SysDict entity, BindingResult bindingResult){
		this.service.save(entity);
        return R.ok();
    }

    /**
     * 修改
     * @param entity
     * @param bindingResult
     * @return
     */
    @PostMapping("update")
    public R update(@Valid @RequestBody SysDict entity, BindingResult bindingResult){
		this.service.updateById(entity);
        return R.ok();
    }

    /**
     * 删除
     * @param params idList: [1,2,3]
     * @return
     */
    @PostMapping("delete")
    public R delete(@RequestBody Map<String, Object> params) {
        if (null != params && !params.isEmpty()) {
            List<Long> idList = JSON.parseArray(params.get("idList").toString(), Long.class);
            this.sysDictDetailService.deleteByDictId(idList);
            this.service.removeByIds(idList);
            return R.ok();
        } else {
            return R.failure();
        }
    }

    /**
     * 根据code获取字典值列表
     * @param dto
     * @return
     */
    @PostMapping(value = "/getDictByCode")
    public Object getDictByCode(@RequestBody SysDictVo dto) {

        Map<String, Object> map = new HashMap<String, Object>() ;

        if(null != dto.getCodes() && dto.getCodes().length > 0) {
            QueryWrapper<SysDict> queryWrapper = new QueryWrapper<SysDict>();
            queryWrapper.in("code", Arrays.asList(dto.getCodes())).select("id", "code", "status");
            List<SysDict> list = this.service.list(queryWrapper);

            for (SysDict dict : list) {
                QueryWrapper<SysDictDetail> queryDictDetails = new QueryWrapper<SysDictDetail>();
                queryDictDetails.eq("dict_id", dict.getId()).orderByDesc("sort").select("id", "label", "value", "selected", "status");
                List<SysDictDetail> dictItems = this.sysDictDetailService.list(queryDictDetails);

                map.put(dict.getCode(), dictItems);
            }
        }

        return R.ok().put("data", map);
    }

}
